package com.college.collegeinfomanage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.college.collegeinfomanage.dto.UserDTO;
import com.college.collegeinfomanage.entity.User;
import com.college.collegeinfomanage.service.UserService;
import com.college.collegeinfomanage.util.Result;
import com.college.collegeinfomanage.vo.UserVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 登录
     * @param
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        if (user.getPassword() == null || "".equals(user.getPassword()) || user.getUsername() == null || "".equals(user.getUsername())){
            return Result.fail("用户名或密码不能为空");
        }
        UserVO userVO = userService.login(user);
        return Result.success(userVO);
    }

    /**
     * 注册
     * @param
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        userService.register(user);
        return Result.success("注册成功");
    }

    @PostMapping("/add")
    public Result add(@RequestBody User user){
        userService.save(user);
        return Result.success("添加成功");
    }

    @GetMapping("/list")
    public Result list(UserDTO userDTO){
        IPage<User> page = userService.getPage(userDTO);
        return Result.success(page);
    }

    @PutMapping("/update")
    public Result update(@RequestBody User user){
        userService.updateById(user);
        return Result.success("修改成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        userService.removeById(id);
        return Result.success("删除成功");
    }
}
